package legumes.client.ui.welcome;

import legumes.client.evt.main.PlaceDeliverySelectionOverviewEvent;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEvent;
import legumes.client.ui.widgets.window.MaximizedStoreWindowMap;
import legumes.client.ui.widgets.window.StoreMapWindow;
import legumes.shared.domain.PlaceDelivery;
import legumes.shared.domain.Store;
import sma.images.slides.client.slider.ImgSlider;

import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.event.MarkerMouseOutHandler;
import com.google.gwt.maps.client.event.MarkerMouseOverHandler;
import com.google.gwt.maps.client.overlay.Marker;
import com.smartgwt.client.widgets.WidgetCanvas;
import com.smartgwt.client.widgets.events.MaximizeClickEvent;
import com.smartgwt.client.widgets.events.MaximizeClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class WelcomePanel extends VLayout {
	
	private StoreMapWindow storeMapWindow;
	private MaximizedStoreWindowMap maximizedWindowMap;
	
	private ImgSlider imgSlider;
	private final String [] imgPathAr = new String[]{
			GWT.getModuleBaseURL() + "/images/siteExplanation/1.png", 
			GWT.getModuleBaseURL() + "/images/siteExplanation/2.png",
			GWT.getModuleBaseURL() + "/images/siteExplanation/3.png",
			GWT.getModuleBaseURL() + "/images/siteExplanation/4.png",
			GWT.getModuleBaseURL() + "/images/siteExplanation/5.png"};
	
	
	public WelcomePanel(){
		super();
        setWidth("600px");
//        setAutoHeight();
        imgSlider = new ImgSlider("400px", "250px", imgPathAr);
        storeMapWindow = new StoreMapWindow(600, 400, null){
        	
        	@Override
        	protected void initWindowProperties(){
        		setShowMinimizeButton(false);
        		 
        		addMaximizeClickHandler(new MaximizeClickHandler() {
        		
        			@Override
        			public void onMaximizeClick(MaximizeClickEvent event) {
        				if(maximizedWindowMap==null){
        					maximizedWindowMap = new MaximizedStoreWindowMap(getMapWidget().getCenter()){
        						
        						@Override
        			        	protected void onStoreSelected(Store st_, PlaceDelivery pl_){
        							WelcomePanel.this.onStoreSelected(st_, pl_);
        			        	}
        						
        						@Override
        						protected void onCloseAction(){
        							super.onCloseAction();
        							//Set the same zoom in the box panel
        							getMapWidget().setZoomLevel(
        									maximizedWindowMap.getMapWidget().getBoundsZoomLevel(maximizedWindowMap.getMapWidget().getBounds()));
        						}
        						
        					};
        					
        					
        				}
        				maximizedWindowMap.show();
        				event.cancel();
        			}
        		});
        	}
        	
        	@Override
        	protected void onStoreSelected(Store st_, PlaceDelivery pl_){
        		WelcomePanel.this.onStoreSelected(st_, pl_);
        	}

			@Override
			protected Marker createMarker(final Store st_, final PlaceDelivery pl_) {
				final Marker m = super.createMarker(st_, pl_);
				m.addMarkerMouseOverHandler(new MarkerMouseOverHandler(){

					@Override
					public void onMouseOver(MarkerMouseOverEvent event) {
						EventBusManager.getInstance().getEventBus().fireEvent(
								new PlaceDeliverySelectionOverviewEvent(st_, pl_));
					}
					
				});
				return m;
			}
        	
        	
        };
        
        setMembers(new WidgetCanvas( imgSlider ), storeMapWindow);
	}
	
	private void onStoreSelected(Store st_, PlaceDelivery pl_){
		if(maximizedWindowMap!=null){
			maximizedWindowMap.setVisible(false);
			maximizedWindowMap.destroy();
		}
		EventBusManager.getInstance().getEventBus().fireEvent(
				new PlaceDeliverySelectionChangedEvent(st_, pl_));
	}

	
}
